/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package layoutitems;

import folder.helper.Folders;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class ImageElement {
    
    ////////////////////////////////////////////////
    //          Fields
    ///////////////////////////////////////////////

    private long id;
    private Image imageElement;
    private AlphaSquare alphaSquare;
    private int xPos = 0;
    private int yPos = 0;
    private boolean dragable = false;
    private boolean marked = false;
    private boolean blocked = false;
    private String name;
    private String imagePath;

    /////////////////////////////////////////////////
    //          CONSTRUCTOR
    /////////////////////////////////////////////////
    public ImageElement() {
    }

    public ImageElement(String filePath, boolean dragable, boolean block) {
        setPicture(filePath);
        this.dragable = dragable;
        this.blocked = block;
    }

    ;
    
    
    //==============================================
    //          GETTER
    //==============================================
    
    public boolean getDragable() {
        return this.dragable;
    }

    public boolean getMarked() {
        return this.marked;
    }

    public Image getImage() {
        return this.imageElement;
    }

    public int[] getCenter() {
        int[] res = new int[2];
        res[0] = (int) (this.imageElement.getWidth(null) / 2);
        res[1] = (int) (this.imageElement.getHeight(null) / 2);
        res[0] += this.xPos;
        res[1] += this.yPos;
        //System.out.println("Widht: " +res[0]+", Height:"+res[1]);
        return res;
    }

    public int getPosX() {
        return this.xPos;
    }

    public int getPosY() {
        return this.yPos;
    }

    public String getName() {
        return this.name;
    }

    public String getImagePath() {
        return this.imagePath;
    }

    public boolean getBlocked() {
        return this.blocked;
    }

    public AlphaSquare getMarkedSquare() {
        this.alphaSquare = new AlphaSquare(this.xPos, this.yPos, this.imageElement.getWidth(null), this.imageElement.getHeight(null));
        return this.alphaSquare;
    }
    
    //============================================
    //          SETTER
    //============================================
    
    public void setId() {
        this.id = (long) Math.random();
    }

    public void setPosX(int x) {
        this.xPos = x;
    }

    public void setPosY(int y) {
        this.yPos = y;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBlocked(boolean block) {
        this.blocked = block;
    }

    public void setDragable(boolean set) {
        this.dragable = set;
        //System.out.println(this.getName()+ "drag" + this.getDragable() + "" + this.getMarked());
    }

    public void setMarked(boolean marked) {
        this.marked = marked;
        //System.out.println(this.getName()+ "drag" + this.getDragable() + "" + this.getMarked());
    }

    public final void setPicture(String filePath) {
        try {
            if(filePath.startsWith("..")){
                filePath =  Folders.main_Folder + filePath.substring(2);
                System.out.println(filePath);
            }
            imageElement = ImageIO.read(new File(filePath));
            this.imagePath = filePath;
        } catch (IOException ioe) {
            System.out.println("IOException in " + this.getClass().getName());
        }
    }
    //===============================================
    //          INNER METHODS
    //===============================================
    public void movePosX(int x) {
        this.xPos += x;
    }

    public void movePosY(int y) {
        this.yPos += y;
    }

    public String writeLayoutElementAsXml() {
        String s = "";

        s += "\t<layoutItem>\n";
        s += "\t\t<name>" + this.name + "</name>\n";
        s += "\t\t<link>" + this.imagePath + "</link>\n";
        s += "\t\t<blocked>";
        if (this.blocked) {
            s += "true";
        } else if (!this.blocked) {
            s += "false";
        }
        s += "</blocked>\n";
        s += "\t</layoutItem>\n";

        return s;
    }

    public String writeCurrentLayoutElementAsXml(int layerPosition) {
        String s = "";
        s += "\t<layoutItem>\n";
        s += "\t\t<id>" + this.id + "</id>\n";
        s += "\t\t<name>" + this.name + "</name>\n";
        s += "\t\t<link>" + this.imagePath + "</link>\n";
        s += "\t\t<blocked>";
        if (this.blocked) {
            s += "true";
        } else if (!this.blocked) {
            s += "false";
        }
        s += "</blocked>\n";
        s += "\t\t<x_position>" + this.xPos + "</x_position>\n";
        s += "\t\t<y_position>" + this.yPos + "</y_position>\n";
        s += "\t\t<layer_position>" + layerPosition + "</layer_position>\n";
        s += "\t</layoutItem>\n";

        return s;
    }

    public void clone(ImageElement copy) {
        this.setName(copy.getName());
        this.setPicture(copy.getImagePath());

    }
}
